package com.qfedu.mtl.controller;

import com.alibaba.fastjson.JSON;
import com.qfedu.mtl.entity.Category;
import com.qfedu.mtl.query.CategoryQuery;
import com.qfedu.mtl.service.CategoryService;
import com.qfedu.mtl.service.impl.CategoryServiceImpl;
import com.qfedu.mtl.util.CharacterUtil;
import com.qfedu.mtl.util.HttpUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.page.Pager;
import com.qfedu.mtl.util.web.annotation.Controller;
import com.qfedu.mtl.util.web.annotation.RequestMapping;
import com.qfedu.mtl.vo.TableVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Date 2023/9/26 21:07
 */
@Controller
@RequestMapping("/category")
public class CategoryController {
    private CategoryService categoryService=new CategoryServiceImpl();

    @RequestMapping("/list.do")
    public void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        String page = StrUtil.ifNull(req.getParameter("page"), "1");
        String size = StrUtil.ifNull(req.getParameter("size"), "10");
        CategoryQuery query = HttpUtil.request2Bean(req, CategoryQuery.class);
        Pager<Category> pager = categoryService.findPage(Integer.valueOf(page), Integer.valueOf(size), query);
        TableVO vo = TableVO.me().setData(pager.getRows()).setCount(pager.getTotal());
        String json= JSON.toJSONString(vo);
        resp.getWriter().write(json);

    }
    @RequestMapping("/find.do")
    public void find(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        String id =req.getParameter("id");
        Category category = categoryService.findById(Integer.valueOf(id));
        HttpUtil.responseSuccess(category,resp);

    }
    @RequestMapping("/add.do")
    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        Category category = HttpUtil.request2Bean(req, Category.class);
        try {
            categoryService.save(category);
            HttpUtil.responseSuccess(null,resp);
        }catch (Exception e){
            HttpUtil.response(null,-1,e.getMessage(),resp);
        }

    }
    @RequestMapping("/delete.do")
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        String id = req.getParameter("id");
        if (StrUtil.isNullOrEmpty(id)){
            HttpUtil.response(null,-1,"请指定需要删除的ID",resp);
            return;
        }
        try {
            categoryService.delete(Integer.valueOf(id));
            HttpUtil.responseSuccess(null,resp);
        }catch (Exception e){
            HttpUtil.response(null,-1,e.getMessage(),resp);
        }
    }
    @RequestMapping("/deleteBatch.do")
    public void deleteBatch(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        String ids = req.getParameter("ids");
        if (StrUtil.isNullOrEmpty(ids)){
            HttpUtil.response(null,-1,"请选择要删除的ID集合",resp);
            return;
        }
        String[] idss = ids.split(",");
        List<Integer> idsss = Stream.of(idss).map(e -> Integer.valueOf(e)).collect(Collectors.toList());

        try {
            categoryService.deleteBcat(idsss);
            HttpUtil.responseSuccess(null,resp);
        }catch (Exception e){
            HttpUtil.response(null,-1,e.getMessage(),resp);
        }

    }
    @RequestMapping("/update.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        Category category = HttpUtil.request2Bean(req, Category.class);
        try {
            categoryService.update(category);
            HttpUtil.responseSuccess(null,resp);
        }catch (Exception e){
            HttpUtil.response(null,-1,e.getMessage(),resp);
        }
    }
}
